package com.mls.fivedistrict.controller.backend;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mls.fivedistrict.common.PageFilter;
import com.mls.fivedistrict.common.RequestHolder;
import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.controller.BaseController;
import com.mls.fivedistrict.exception.CustomerException;
import com.mls.fivedistrict.pojo.Notify;
import com.mls.fivedistrict.server.NotifyService;
import com.mls.fivedistrict.util.validator.BeanValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 公告管理 前端控制器
 * </p>
 *
 * @author Five Team
 * @since 2018-07-06
 */
@RestController("backendNotifyController")
@RequestMapping("/api/manager")
public class NotifyController extends BaseController {
    @Autowired
    NotifyService notifyService;

    @PostMapping("/v1/notify")
    public ServiceResponse addNotify(@RequestBody Notify notify) {
        BeanValidator.check(notify);
        boolean insert = notifyService.insert(notify);
        if (insert) {
            return ServiceResponse.createSuccessResponse("添加公告成功");
        }
        return ServiceResponse.createErrorResponse("添加公告失败");
    }

    @DeleteMapping("/v1/notify/{notifyId}")
    public ServiceResponse deleteNotify(@PathVariable Long notifyId) {
        Optional.ofNullable(notifyId).orElseThrow(() -> new CustomerException("公告id不能为空"));
        Notify notify = new Notify();
        notify.setId(notifyId);
        notify.setDelete(true);
        boolean delete = notifyService.updateById(notify);
        if (delete) {
            return ServiceResponse.createSuccessResponse("删除公告成功");
        }
        return ServiceResponse.createErrorResponse("删除公告失败");
    }

    @PutMapping("/v1/notify/{notifyId}")
    public ServiceResponse updateNotify(@PathVariable Long notifyId, @RequestBody Notify notify) {
        Optional.ofNullable(notifyId).orElseThrow(() -> new CustomerException("公告id不能为空"));
        Optional.ofNullable(notify).orElseThrow(() -> new CustomerException("公告内容不能为空"));
        notify.setId(notifyId);
        boolean update = notifyService.updateById(notify);
        if (update) {
            return ServiceResponse.createSuccessResponse("修改公告成功");
        }
        return ServiceResponse.createErrorResponse("修改公告失败");
    }

    @GetMapping("/v1/notify/{notifyId}")
    public ServiceResponse getNotify(@PathVariable Long notifyId) {
        Optional.ofNullable(notifyId).orElseThrow(() -> new CustomerException("公告id不能为空"));
        Notify notify = notifyService.selectById(notifyId);
        if (null != notify) {
            return ServiceResponse.createSuccessResponse(notify);
        }
        return ServiceResponse.createErrorResponse("查无此公告");
    }

    @GetMapping("/v1/notifies")
    public ServiceResponse getNotifyList(){
        PageFilter pageFilter = RequestHolder.getPageFilter();
        PageHelper.startPage(pageFilter.getPageNum(),pageFilter.getPageSize());
        List selectList = notifyService.selectList(pageFilter.getWrapper());
        PageInfo<Notify> notifyPageInfo = new PageInfo<Notify>(selectList, 5);
        return ServiceResponse.createSuccessResponse(notifyPageInfo);
    }
}

